<?php

/**
 * CollectionRelation form.
 *
 * @package    travide
 * @subpackage form
 * @author     Travide Project Group
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class CollectionRelationForm extends BaseCollectionRelationForm {
    public function postValidateFields($validator, $values) {
        /**
         * Determine if any of the required values are missing
         * this cannot be done normally a number of new object forms will also
         * be retured with an edit and these will be deleted later
         */
        if (($values['relationTypeUin'] != '') && ($values['relatedItemUin'] == '')) {
            throw new sfValidatorError($validator, 'Related item not specified for relation');
        }

        if (($values['relationTypeUin'] == '') && ($values['relatedItemUin'] != '')) {
            throw new sfValidatorError($validator, 'Relation type not specified for relation');
        }

        return $values;
    }
    
    public function configure() {
        /**
         * Set the widget formatter to InlineTable
         */
        $this->getWidgetSchema()->addFormFormatter('InlineTable', new InlineTableFormSchemaFormatter($this->getWidgetSchema()));
        $this->getWidgetSchema()->setFormFormatterName('InlineTable');
        /**
         * For new objects (i.e. blank forms) allow an empty option to force
         * the user to select - or leave blank so some forms can interpret this
         * as ignore or delete
         */
        $this->getValidatorSchema()->setPostValidator(new sfValidatorCallback(array('callback' => array($this, 'postValidateFields'))));
        /**
         * Configure Relation Type Field
         */
        $w = $this->getWidget('relationTypeUin');
        $w->setOption('add_empty', $this->isNew());
        $w->setLabel('Related as');
        $this->getValidator('relationTypeUin')->setOption('required', !$this->isNew());
        /**
         * Collection this relation relates too
         */
        //$w = $this->getWidget('relatedItemUin');
        /**
         * IF the items is new then add an empty row
         * OR if the relatedCollection is Archived (i.e. won't be available in the list
         * add an empty item as well so the relation becomes empty and forces the user
         * to update the Collection
         */
        //$w->setOption('add_empty', $this->isNew()||$this->getObject()->getRelatedItem()->getDeletedAt());
        /**
         * Create the related Item UIN field as a text field
         * Jquery on the page will hide this and replace with the auto-compelete
         * field which will then populate the UIN Over to this.
         */
        /**
         * update need to use inputHidden for now otherwise symfony gets
         * smart and shows the object name not its ID,
         * Will be fine when the tables are de-coupled however
         */
        $w = new sfWidgetFormInputHidden();

        $w->setLabel('Related to');
        $w->setAttribute('class', 'relatedItemUin');
        $this->setWidget('relatedItemUin', $w);
        /**
         * Set this option to use the CollectionItemTable's addNonARchivedQuery
         * method - this will remove all archived queries from the drop
         * down list
         */
        //$w->setOption('table_method','getCollectionListQuery');
        $this->getValidator('relatedItemUin')->setOption('required', !$this->isNew());

        $w = new sfWidgetFormInputText();
        $w->setLabel('Related to');
        $w->setAttribute('class','relatedItemName');
        $this->setWidget('relatedItemName',$w);
        $this->setDefault('relatedItemName', $this->getObject()->getRelatedItem()->__toString());
        $this->setValidator('relatedItemName', new sfValidatorPass());


        if (!$this->isNew) {
            /**
             * Add the Delete Relation Widget - relationDelete
             * Allows user to select to delete the relation on edit
             */
            $w = new sfWidgetFormInputCheckbox();
            $w->setLabel('Delete');
            $this->setWidget('relationDelete', $w);
            $this->setValidator('relationDelete', new sfValidatorBoolean());
        }
        /**
         * Remove thisCollectionUin - as we don't want this to show up on the
         * new or edit forms for Collections - it will be set automatically
         */
        unset($this['thisCollectionUin']);
    }
}
